package com.huayanginfo.saas.email;

import java.io.StringWriter;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.HtmlEmail;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;

import com.huayanginfo.saas.support.ContextUtils;

public class EmailService {

	private EmailService() {
		
	}

	/**
	 * from and name can be null, which will be the default
	 * @param to should not be null
	 * @param subject
	 * @param message
	 * @param from
	 * @param name
	 * @throws EmailException
	 * @throws UnsupportedEncodingException
	 */
	public static void sendHTMLEmail(String to, String subject, String message, String from, String name)
			throws EmailException, UnsupportedEncodingException {
		HtmlEmail email = EmailClient.getInstance();
		if (from != null) {
			email.setFrom(from, name);
		}
		if (to == null || subject == null || message == null) {
			throw new EmailException("Email field: to or subject or message should not be null, please check!");
		}
		email.addTo(to);
		email.setSubject(subject);
		email.addPart(message, "text/html;charset=utf-8");
		email.send();
	}

	public static void sendRegisterCompany(String email, String company, String verifyCode) throws UnsupportedEncodingException, EmailException {
		Template template = null;
		VelocityContext context = new VelocityContext();
		context.put("hostURL", ContextUtils.getHostURL());
		context.put("context", ContextUtils.getContext());
		context.put("hostName", ContextUtils.getHostName());
		context.put("action", ContextUtils.getActionExtention());
		context.put("email", URLEncoder.encode(email, "UTF-8"));
		context.put("companyName", URLEncoder.encode(company, "UTF-8"));
		context.put("verifyCode", verifyCode);
		template = Velocity.getTemplate("vm/register_company.vm");
		StringWriter stringWriter = new StringWriter();
		template.merge(context, stringWriter);
		String message = stringWriter.toString();
		String subject = "注册" + ContextUtils.getHostName();
		sendHTMLEmail(email, subject, message, null,null);
	}

	public static void sendResetPassword(String email, String verifyCode) throws UnsupportedEncodingException, EmailException {
		Template template = null;
		VelocityContext context = new VelocityContext();
		context.put("hostURL", ContextUtils.getHostURL());
		context.put("hostName", ContextUtils.getHostName());
		context.put("context", ContextUtils.getContext());
		context.put("action", ContextUtils.getActionExtention());
		context.put("email", URLEncoder.encode(email, "UTF-8"));
		context.put("verifyCode", verifyCode);
		template = Velocity.getTemplate("vm/reset_password.vm");
		StringWriter stringWriter = new StringWriter();
		template.merge(context, stringWriter);
		String message = stringWriter.toString();
		String subject = ContextUtils.getHostName() + "重置密码";
		sendHTMLEmail(email, subject, message, null,null);
	}
	
//	public static void main(String[] args) {
//		Velocity.setProperty(Velocity.RESOURCE_LOADER, "class");
//		Velocity.setProperty("class.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
//		VelocityContext context = new VelocityContext();
//		context.put("hostURL", "10.3.1.20:11");
//		context.put("context", "");
//		context.put("action", "aaaa");
//		context.put("email", "aaa");
//		context.put("companyName", "hyinfo");
//		context.put("verifyCode", "aaaa");
//		Template template = Velocity.getTemplate("vm/register_company.vm");
//		StringWriter stringWriter = new StringWriter();
//		template.merge(context, stringWriter);
//		System.out.println(stringWriter.toString());
//	}
}
